package spacecrack;

import java.util.ArrayList;
import java.util.Collection;

import o.json.JSONFactory;
import o.json.JSONWriter;

public class UnitManager {
	private final JSONFactory factory = new JSONFactory();
	private final Collection<Unit> units = new ArrayList<Unit>();

	public Collection<Unit> getUnits() { return units; }
	public void addUnit(Unit unit) { units.add(unit); }

	public static class Unit {
		private String id;
		private String name;
		private int posx, posy;
		private int ships;
		private Collection<Target> targets = new ArrayList<Target>();

		public Unit(String id, String name, int posx, int posy, int ships) {
			this.id = id;
			this.name = name;
			this.posx = posx;
			this.posy = posy;
			this.ships = ships;
		}

		public String getId() { return id; }
		public String getName() { return name; }
		public int getPosX() { return posx; }
		public int getPosY() { return posy; }
		public int getShips() { return ships; }
		public Collection<Target> getTargets() { return targets; }
		public void addTarget(Target target) { targets.add(target); }
	}

	public static class Target {
		private String id;
		private int posx, posy;

		public Target(String id, int posx, int posy) {
			this.id = id;
			this.posx = posx;
			this.posy = posy;
		}

		public String getId() { return id; }
		public int getPosX() { return posx; }
		public int getPosY() { return posy; }
	}

	public String asJSONValue() {
		final JSONWriter writer = new JSONWriter();
		writer.write(factory.create(this));
		return writer.toString();
	}
}
